package design_pattern.ObserveThread.basic;

import design_pattern.ObserveThread.basic.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/30 10:34
 * @Version 1.0
 */
public class Subject {
    private List<Observer> observerList = new ArrayList<>();

    //事件源发生变化的一个东西。
    private int state;
    public int getState(){
        return this.state;
    }
    public void setState(int state){
        if(this.state == state){
            return;
        }
        this.state = state;
        notifyAllObserver();
    }
    public void attach(Observer observer){
        observerList.add(observer);
    }
    private void notifyAllObserver(){
        observerList.stream().forEach(Observer::update);
    }


}
